#!/usr/bin/python
#-*- coding: utf-8 -*-
#title			:yoghourt.py
#description	:2013 ComputerVision Schwanecke Uebungsblatt 01, Yoghourtbecher
#author			:Pascal Trebert
#date			:20131016
#version		:1.0
#usage			:python yoghourt.py
#notes			:
#python_version	:Python 2.7.3
#==============================================================================

import sys
import pprint as pp
import numpy as np
import cv2


left, cover, right = 'cover_left.JPG',  'cover_top.JPG', 'cover_right.JPG'
pics = [left, cover, right]

    
def file_to_image(files # list of filenames
                 ):
    images = []
    for f in files:
        images.append(cv2.imread(f, cv2.CV_LOAD_IMAGE_COLOR))
    return images


def color_2_gray(images # list of cv2 image files
                 ):
    images_gray = []
    for image in images:
        images_gray.append(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))
    return images_gray


def show(img, # numpyarray
         windowname # unique string
         ):
    cv2.namedWindow(windowname, cv2.CV_WINDOW_AUTOSIZE)
    cv2.imshow(windowname, img)


def show_all(lis # images
                        ):
    if len(lis) != 3:
        raise Exception, "Es werden genau 3 images [left, top, right] erwartet in der liste!"
    for tup in zip(lis, pics):
        show(*tup)


def p(i):
    pp.pprint(i.tolist()[0][:30])


def luminance(left, top, right):
    return (left/3+right/3+top/3)


def bias(left, right):
    return (left/2 - right/2)  + 127


def brightness(left, top, right):
    return top/2 - ((left/2 + right/2) / 2) + 127


if __name__=="__main__":

    # Bilder einlesen
    images = file_to_image(pics)

    # in SW konvertieren
    images_gray = color_2_gray(images)

    # i_luminance berr.
    i_luminance = luminance(*images_gray)
    show(i_luminance, 'i_luminance')
    cv2.imwrite("i_luminance.JPG", i_luminance)

    # i_bias berr.
    i_bias = bias(images_gray[0], images_gray[2])
    show(i_bias, 'i_bias')
    cv2.imwrite("i_bias.JPG", i_bias)
    
    # i_brightness berr.
    i_brightness = brightness(*images_gray)
    show(i_brightness, 'i_brightness')
    cv2.imwrite("i_brightness.JPG", i_brightness)

    
    cv2.waitKey(0) # unendlich warten auf Tastendruck
    cv2.destroyAllWindows()
    

